package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Floor;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.service.IFloorService;
import com.zzyl.nursing.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.zzyl.common.constant.CacheConstants.IOT_DEVICE_LAST_DATA;

/**
 * 楼层Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements IFloorService {
    @Autowired
    private FloorMapper floorMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询楼层
     *
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id) {
        return getById(id);
    }

    /**
     * 新增楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor) {
        return save(floor) ? 1 : 0;
    }

    /**
     * 修改楼层
     *
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor) {
        return updateById(floor) ? 1 : 0;
    }

    /**
     * 批量删除楼层
     *
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除楼层信息
     *
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 根据床位状态 查询所有楼层数据（包含楼层&房间&床位）（Day6 -课堂6）
     *
     * @param status
     * @return
     */
    @Override
    public List<TreeVo> getRoomAndBedByBedStatus(Integer status) {
        return floorMapper.getRoomAndBedByBedStatus(status);
    }

    /**
     * 查询智能楼层
     *
     * @return
     */
    @Override
    public List<FloorVo> getAllFloors() {
        return floorMapper.getAllFloors();
    }

    /**
     * 获取所有房间（智能床位）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //查询数据封装到roomVo对象当中
        List<RoomVo> roomVoList = floorMapper.getRoomsWithDeviceByFloorId(floorId);
        roomVoList.forEach(r -> {
            List<DeviceInfo> deviceVos = r.getDeviceVos();
            List<BedVo> bedVoList = r.getBedVoList();
            if (ObjectUtil.isNotEmpty(deviceVos)) {
                deviceVos.forEach(d -> {
                    String iotId = d.getIotId();
                    //从redis中获取数据
                    List<DeviceData> deviceData = (List<DeviceData>) redisTemplate.opsForHash().get(IOT_DEVICE_LAST_DATA, iotId);
                    d.setDeviceDataVos(deviceData);
                });
            }
            bedVoList.forEach(b -> {
                List<DeviceInfo> deviceVos1 = b.getDeviceVos();
                deviceVos1.forEach(d -> {
                    String iotId = d.getIotId();
                    List<DeviceData> deviceData = (List<DeviceData>) redisTemplate.opsForHash().get(IOT_DEVICE_LAST_DATA, iotId);
                    d.setDeviceDataVos(deviceData);
                });
            });
        });
        return roomVoList;
    }

}
